﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Maticsoft.Common;
using LTP.Accounts.Bus;
namespace XDSoft.Web.TB_Brand
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                XDSoft.BLL.TB_Options bll = new XDSoft.BLL.TB_Options();
                this.ddlStatus.AppendDataBoundItems = true;
                this.ddlStatus.DataSource = bll.GetModelList("parent='" + EnumCommon.OptionsType.BrandStatus.ToString() + "'");
                this.ddlStatus.DataTextField = "name";
                this.ddlStatus.DataValueField = "id";
                this.ddlStatus.DataBind();

                this.ddlBrandType.AppendDataBoundItems = true;
                this.ddlBrandType.DataSource = bll.GetModelList("parent='" + EnumCommon.OptionsType.BrandType.ToString() + "'");
                this.ddlBrandType.DataTextField = "name";
                this.ddlBrandType.DataValueField = "id";
                this.ddlBrandType.DataBind();

                this.ddlTypeCode.AppendDataBoundItems = true;
                this.ddlTypeCode.DataSource = bll.GetModelList("parent='" + EnumCommon.OptionsType.InternationalCode.ToString() + "'");
                this.ddlTypeCode.DataTextField = "name";
                this.ddlTypeCode.DataValueField = "id";
                this.ddlTypeCode.DataBind();

                XDSoft.BLL.TB_Clients cBll = new XDSoft.BLL.TB_Clients();
                this.ddlClient.Items.Add(new ListItem("", ""));
                this.ddlClient.AppendDataBoundItems = true;
                this.ddlClient.DataSource = cBll.GetModelList("");
                this.ddlClient.DataTextField = "clientName";
                this.ddlClient.DataValueField = "id";
                this.ddlClient.DataBind();


                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int id = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(id);
                }
            }
        }

        private void ShowInfo(int id)
        {
            XDSoft.BLL.TB_Brand bll = new XDSoft.BLL.TB_Brand();
            XDSoft.Model.TB_Brand model = bll.GetModel(id);
            this.lblid.Text = model.id.ToString();
            if (!string.IsNullOrEmpty(model.icon))
            {
                string vPath = WebCommon.GetVPath(this.Page);
                this.hddIcon.Value = model.icon;
                Maticsoft.Common.MessageBox.ResponseScript(this, "show('" + vPath + model.icon.Replace("Big", "Small").Replace(@"\", @"/") + "')");
            }

            this.txtregCode.Text = model.regCode;
            if (model.applyDate != null)
                this.txtapplyDate.Text = Convert.ToDateTime(model.applyDate).ToString("yyyy-MM-dd");
            this.txtapplyManName.Text = model.applyManName;
            this.txtapplyManAddress.Text = model.applyManAddress;
            this.txtapplyManEnName.Text = model.applyManEnName;
            this.txtapplyManEnAddress.Text = model.applyManEnAddress;
            if (model.specialStartDate != null)
                this.txtspecialStartDate.Text = Convert.ToDateTime(model.specialStartDate).ToString("yyyy-MM-dd");
            if (model.specialEndDate != null)
                this.txtspecialEndDate.Text = Convert.ToDateTime(model.specialEndDate).ToString("yyyy-MM-dd");
            //this.txtbrandType.Text = model.brandType;
            this.txtclientService.Text = model.clientService;
            this.txtproductList.Text = model.productList;
            if (model.agentFee != null)
                this.txtagentFee.Text = model.agentFee.ToString();
            if (model.netApplyDate != null)
                this.txtnetApplyDate.Text = Convert.ToDateTime(model.netApplyDate).ToString("yyyy-MM-dd");
            if (model.docSubmitDate != null)
                this.txtdocSubmitDate.Text = Convert.ToDateTime(model.docSubmitDate).ToString("yyyy-MM-dd");
            if (model.assignDate != null)
                this.txtassignDate.Text = Convert.ToDateTime(model.assignDate).ToString("yyyy-MM-dd");
            //this.txtstatus.Text = model.status;
            this.ddlStatus.SelectedValue = model.status;
            this.ddlTypeCode.SelectedValue = model.typeCode;
            this.ddlBrandType.SelectedValue = model.brandType;
            this.ddlClient.SelectedValue = model.client.ToString();
            this.txtmemo.Text = model.memo;
            if (model.serviceExpire != null)
                this.txtserviceExpire.Text = Convert.ToDateTime(model.serviceExpire).ToString("yyyy-MM-dd");

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            //if (this.txticon.Text.Trim().Length == 0)
            //{
            //    strErr += "商标图样不能为空！\\n";
            //}
            if (this.txtregCode.Text.Trim().Length == 0)
            {
                strErr += "商标注册号不能为空！\\n";
            }

            if (this.txtapplyDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtapplyDate.Text))
            {
                strErr += "申请日期格式错误！\\n";
            }
            if (this.txtspecialStartDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtspecialStartDate.Text))
            {
                strErr += "专用权期限开始日期格式错误！\\n";
            }
            if (this.txtspecialEndDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtspecialEndDate.Text))
            {
                strErr += "专用权期限结束日期格式错误！\\n";
            }
            if (string.IsNullOrEmpty(this.ddlBrandType.SelectedValue))
            {
                strErr += "商标类型不能为空！\\n";
            }
            if (this.txtproductList.Text.Trim().Length == 0)
            {
                strErr += "商品服务列表不能为空！\\n";
            }
            if (this.txtagentFee.Text.Trim().Length > 0 && !PageValidate.IsDecimal(txtagentFee.Text))
            {
                strErr += "代理费用格式错误！\\n";
            }
            if (this.txtnetApplyDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtnetApplyDate.Text))
            {
                strErr += "网上申请日期格式错误！\\n";
            }
            if (this.txtdocSubmitDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtdocSubmitDate.Text))
            {
                strErr += "资料交局日期格式错误！\\n";
            }
            if (this.txtassignDate.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtassignDate.Text))
            {
                strErr += "商标委托受理通知书下发日期格式错误！\\n";
            }
            if (string.IsNullOrEmpty(this.ddlStatus.SelectedValue))
            {
                strErr += "商标状态不能为空！\\n";
            }
            if (this.txtserviceExpire.Text.Trim().Length > 0 && !PageValidate.IsDateTime(txtserviceExpire.Text))
            {
                strErr += "服务到期日格式错误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            UploadImage ui = new UploadImage();
            string uploadMsg = string.Empty;
            string iconPath = string.Empty;
            if (this.FileUpload1.HasFile)//上传了图标
            {
                ui.ImgThumbnail = true;
                ui.ImgLogo = false;
                iconPath = ui.UpLoadFiles(this.FileUpload1, out uploadMsg);
                if (string.IsNullOrEmpty(iconPath))
                {
                    MessageBox.Show(this, uploadMsg);
                    return;
                }
            }

            int id = int.Parse(this.lblid.Text);
            string regCode = this.txtregCode.Text;
            //DateTime? applyDate = PageValidate.IsDateTime(this.txtapplyDate.Text) ? Convert.ToDateTime(this.txtapplyDate.Text) : null;
            string applyManName = this.txtapplyManName.Text;
            string applyManAddress = this.txtapplyManAddress.Text;
            string applyManEnName = this.txtapplyManEnName.Text;
            string applyManEnAddress = this.txtapplyManEnAddress.Text;
            //DateTime specialStartDate = DateTime.Parse(this.txtspecialStartDate.Text);
            //DateTime specialEndDate = DateTime.Parse(this.txtspecialEndDate.Text);
            //string brandType = this.txtbrandType.Text;
            string clientService = this.txtclientService.Text;
            string productList = this.txtproductList.Text;
            //decimal agentFee = decimal.Parse(this.txtagentFee.Text);
            //DateTime netApplyDate = DateTime.Parse(this.txtnetApplyDate.Text);
            //DateTime docSubmitDate = DateTime.Parse(this.txtdocSubmitDate.Text);
            //DateTime assignDate = DateTime.Parse(this.txtassignDate.Text);
            //string status = this.txtstatus.Text;
            string memo = this.txtmemo.Text;


            XDSoft.Model.TB_Brand model = new XDSoft.Model.TB_Brand();
            model.id = id;
            if (!string.IsNullOrEmpty(iconPath))
                model.icon = iconPath;
            else
                model.icon = string.IsNullOrEmpty(this.hddIcon.Value) ? null : this.hddIcon.Value;
            model.regCode = regCode;
            model.typeCode = this.ddlTypeCode.SelectedValue;
            if (PageValidate.IsDateTime(this.txtapplyDate.Text))
                model.applyDate = Convert.ToDateTime(this.txtapplyDate.Text);
            else
                model.applyDate = null;
            model.applyManName = applyManName;
            model.applyManAddress = applyManAddress;
            model.applyManEnName = applyManEnName;
            model.applyManEnAddress = applyManEnAddress;
            if (PageValidate.IsDateTime(this.txtspecialStartDate.Text))
                model.specialStartDate = Convert.ToDateTime(this.txtspecialStartDate.Text);
            else
                model.specialStartDate = null;
            if (PageValidate.IsDateTime(this.txtspecialEndDate.Text))
                model.specialEndDate = Convert.ToDateTime(this.txtspecialEndDate.Text);
            else
                model.specialEndDate = null;
            model.brandType = this.ddlBrandType.SelectedValue;
            model.clientService = clientService;
            model.productList = productList;
            if (this.txtagentFee.Text.Length > 0)
                model.agentFee = decimal.Parse(this.txtagentFee.Text);
            else
                model.agentFee = null;
            if (PageValidate.IsDateTime(this.txtnetApplyDate.Text))
                model.netApplyDate = Convert.ToDateTime(this.txtnetApplyDate.Text);
            else
                model.netApplyDate = null;
            if (PageValidate.IsDateTime(this.txtdocSubmitDate.Text))
                model.docSubmitDate = Convert.ToDateTime(this.txtdocSubmitDate.Text);
            else
                model.docSubmitDate = null;
            if (PageValidate.IsDateTime(this.txtassignDate.Text))
                model.assignDate = Convert.ToDateTime(this.txtassignDate.Text);
            else
                model.assignDate = null;
            model.status = this.ddlStatus.SelectedValue;
            if (!string.IsNullOrEmpty(this.ddlClient.SelectedValue))
                model.client = Convert.ToInt32(this.ddlClient.SelectedValue);
            model.memo = memo;
            if (PageValidate.IsDateTime(this.txtserviceExpire.Text))
                model.serviceExpire = Convert.ToDateTime(this.txtserviceExpire.Text);
            else
                model.serviceExpire = null;

            XDSoft.BLL.TB_Brand bll = new XDSoft.BLL.TB_Brand();
            if (bll.Update(model))
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this.Page, "保存成功", WebCommon.GetPageUrl());
            }
            else
                Maticsoft.Common.MessageBox.Show(this, "保存失败！");
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }

        protected void btnDelIcon_Click(object sender, EventArgs e)
        {
            this.hddIcon.Value = null;
            Maticsoft.Common.MessageBox.ResponseScript(this, "delIcon()");
        }
    }
}
